package com.zatech.cgnci.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.*;
import com.zatech.cgnci.project.entity.CgnciAnnualInsurePlanDO;
import com.zatech.cgnci.project.entity.CgnciSolvencyAdequacyDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.mapper.CgnciReinsurerRatingMapper;
import com.zatech.cgnci.project.service.*;
import com.zatech.cgnci.project.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 风险阈值表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2025-02-26
 */
@RestController
@RequestMapping("/api/riskManagement")
@Api(produces = "application/json", tags = "风险预警管理接口文档")
@RequiredArgsConstructor
public class CgnciRiskThresholdController {
    @Resource
    private CgnciRiskThresholdService riskThresholdService;
    @Resource
    private CgnciCombinedCostService combinedCostService;
    @Resource
    private CgnciReinsurerConcentrationService reinsurerConcentrationService;
    @Resource
    private CgnciSolvencyAdequacyService solvencyAdequacyService;
    @Resource
    private CgnciReinsurerRatingService ratingService;

    @GetMapping("/threshold/list")
    @ApiOperation("查询阈值")
    public List<CgnciRiskThresholdVO> queryList() {
        return riskThresholdService.selectData();
    }

    @PostMapping("/threshold/update")
    @ApiOperation(value = "预警阈值修改")
    public void update(@RequestBody RiskThresholdDTO riskThresholdDTO) {
        riskThresholdService.updateBatchById(riskThresholdDTO.getThresholdDOS());
    }

    @GetMapping("/combined/list")
    @ApiOperation("查询综合成本率")
    public List<CombinedCostVO> listCombined(@RequestParam(value = "annual", required = false) String annual) {
        return combinedCostService.listCombined(annual);
    }

    @PostMapping("/combined/saveOrUpdate")
    @ApiOperation(value = "综合成本率新增或修改")
    @Transactional(rollbackFor = Exception.class)
    public Long saveOrUpdateCombined(@RequestBody CombinedCostSaveOrUpdateDTO saveOrUpDTO) {
        return combinedCostService.saveOrUpdateCombined(saveOrUpDTO);
    }

    @GetMapping("/combined/deleteById")
    @ApiOperation(value = "删除综合成本率")
    public void deleteCombined(@RequestParam(value = "id") Long id) {
        combinedCostService.removeById(id);
    }

    @GetMapping("/combined/deleteByIds")
    @ApiOperation(value = "批量删除综合成本率")
    public void batchDeleteCombined(@RequestParam(value = "ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
        combinedCostService.removeByIds(idList);
    }


    @PostMapping("/concentration/page")
    @ApiOperation("分页查询转分再保人集中度")
    public IPage<CgnciReinsurerConcentrationVO> pageConcentration(@RequestBody CgnciReinsurerConcentrationDTO dto) {
        return reinsurerConcentrationService.pageConcentration(dto);
    }


    @GetMapping("/adequacy/list")
    @ApiOperation("查询偿付能力充足率")
    public List<SolvencyAdequacyVO> listAdequacy(@RequestParam(value = "annual", required = false) String annual) {
        return solvencyAdequacyService.listAdequacy(annual);
    }

    @PostMapping("/adequacy/saveOrUpdate")
    @ApiOperation(value = "偿付能力充足率新增或修改")
    @Transactional(rollbackFor = Exception.class)
    public Long saveOrUpdateSolvencyAdequacy(@RequestBody SolvencyAdequacySaveOrUpDTO saveOrUpDTO) {
        return solvencyAdequacyService.saveOrUpdateSolvencyAdequacy(saveOrUpDTO);
    }

    @GetMapping("/adequacy/deleteById")
    @ApiOperation(value = "删除偿付能力充足率")
    public void deleteAdequacy(@RequestParam(value = "id") Long id) {
        solvencyAdequacyService.removeById(id);
    }

    @GetMapping("/adequacy/deleteByIds")
    @ApiOperation(value = "批量删除偿付能力充足率")
    public void batchDeleteAdequacy(@RequestParam(value = "ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
        solvencyAdequacyService.removeByIds(idList);
    }


    @PostMapping("/rating/page")
    @ApiOperation("分页查询再保人评级")
    public IPage<CgnciReinsurerRatingVO> pageRating(@RequestBody CgnciReinsurerRatingDTO ratingDTO) {
        return ratingService.pageRating(ratingDTO);
    }

}

